package com.master.lambda;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class LambdaAnonymousTest {

  public static void main(String[] args) {
    List<Cat> catList = new ArrayList<>();
// 请无视这些数据的合理性，我乱写的
    catList.add(new Cat("cat1", 10.3, 3.6));
    catList.add(new Cat("cat2", 9.3, 4.6));
    catList.add(new Cat("cat3", 9.5, 4.0));


    // 指定用高度来排序
        /*Collections.sort(catList, new Comparator<Cat>() {
            @Override
            public int compare(Cat o1, Cat o2) {
                if (o1.getHeight() > o2.getHeight()) {
                    return 1;
                } else if (o1.getHeight() < o2.getHeight()) {
                    return -1;
                } else {
                    return 0;
                }
            }
        });
*/
    catList.forEach((cat) -> System.out.println(cat));
    //lambda写法
    Collections.sort(catList, (o1, o2) -> {
      if (o1.getHeight() > o2.getHeight()) {
        return 1;
      } else if (o1.getHeight() < o2.getHeight()) {
        return -1;
      } else {
        return 0;
      }
    });
    System.out.println("after sort");
    catList.forEach((cat) -> System.out.println(cat));

  }
}
